/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author 张丁方
 * @CreateTime 2017-3-23 11:48:01
 */
package com.hyyt.shopjsp.home.service.imp;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hyyt.shopjsp.home.dao.ShopHomeMiddleCategoryTabDAO;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.hyyt.shopjsp.home.dao.ShopHomeMiddleCategoryDAO;
import com.hyyt.shopjsp.home.pojo.ShopHomeMiddleCategory;
import com.hyyt.shopjsp.home.service.IShopHomeMiddleCategoryService;

@Service
public class ShopHomeMiddleCategoryService implements IShopHomeMiddleCategoryService {
    private static  final Logger logger = Logger.getLogger(IShopHomeMiddleCategoryService.class);

    @Resource
    private ShopHomeMiddleCategoryDAO shopHomeMiddleCategoryDAO;

    @Resource
    private ShopHomeMiddleCategoryTabDAO shopHomeMiddleCategoryTabDAO;

    /**
     * 保存或更新SHOP_首页中间分类
     *
     * @param shopHomeMiddleCategory SHOP_首页中间分类
     * @return 保存后的SHOP_首页中间分类
     */
    @Override
    public ResultMsg saveOrUpdateShopHomeMiddleCategory(ShopHomeMiddleCategory shopHomeMiddleCategory) {
        if(Utils.objectIsNotEmpty(shopHomeMiddleCategory)){
            if(Utils.objectIsNotEmpty(shopHomeMiddleCategory.getCategoryId())){//修改
                shopHomeMiddleCategoryDAO.update(shopHomeMiddleCategory);
            }else{//添加
                ShopHomeMiddleCategory parentCategory = shopHomeMiddleCategoryDAO.load(shopHomeMiddleCategory.getParentId());
                if(Utils.objectIsNotEmpty(parentCategory)) {//父节点不为空
                    if (parentCategory.getIsLeaf().compareTo(0) != 0) {
                        parentCategory.setIsLeaf(0);//修改父节点为非子节点
                        shopHomeMiddleCategoryDAO.update(parentCategory);
                    }
                    shopHomeMiddleCategory.setLevelCode(parentCategory.getLevelCode() + 1);
                }else{//父节点为空，当前节点为一级节点
                    shopHomeMiddleCategory.setParentId(0);
                    shopHomeMiddleCategory.setLevelCode(1);
                }
                shopHomeMiddleCategory.setIsLeaf(1);
                shopHomeMiddleCategoryDAO.insert(shopHomeMiddleCategory);
            }
            return new ResultMsg(true);
        }else{
            return new ResultMsg(false);
        }
    }

    /**
     * 删除指定ID的SHOP_首页中间分类
     *
     * @param categoryId SHOP_首页中间分类ID
     */
    @Override
    public ResultMsg deleteShopHomeMiddleCategory(Integer categoryId) {
        ResultMsg result = new ResultMsg(false);
        if (Utils.objectIsNotEmpty(categoryId)) {
            //1判断当前分类下面是否有内容，有内容则不能删除
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("categoryId", categoryId);
            int count = shopHomeMiddleCategoryTabDAO.count(param);
            if (count == 0) {//为0表示无内容信息
                ShopHomeMiddleCategory shopHomeMiddleCategory = shopHomeMiddleCategoryDAO.load(categoryId);
                if (Utils.objectIsNotEmpty(shopHomeMiddleCategory)) {
                    shopHomeMiddleCategoryDAO.delete(categoryId); //删除当前分类
                    param.clear();
                    param.put("parentId", shopHomeMiddleCategory.getParentId());
                    int count1 = this.countShopHomeMiddleCategory(param);
                    //判断此分类是否有兄弟，等于1 即没有兄弟，那么父节点分类即成为叶子节点
                    if (count1 == 0) {
                        ShopHomeMiddleCategory parentCategory = shopHomeMiddleCategoryDAO.load(shopHomeMiddleCategory.getParentId());
                        parentCategory.setIsLeaf(1);  //改为叶子节点
                        shopHomeMiddleCategoryDAO.update(parentCategory);
                    }
                    result.setSuccess(true);
                }
            } else {
                result.setSuccess(false);
            }
        }else {
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 删除指定ID集合的SHOP_首页中间分类
     *
     * @param ids SHOP_首页中间分类ID集合
    */
    public void deleteShopHomeMiddleCategory(Collection<Integer> ids){
        shopHomeMiddleCategoryDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个SHOP_首页中间分类
     *
     * @param id SHOP_首页中间分类ID
     * @return SHOP_首页中间分类
     */
    @Override
    public ShopHomeMiddleCategory loadShopHomeMiddleCategory(Integer id) {
        return shopHomeMiddleCategoryDAO.load(id);
    }

    /**
    * 查询所有的SHOP_首页中间分类，通常有最多取30000条记录的限制
    *
    * @return 所有的SHOP_首页中间分类，通常有最多取30000条记录的限制
    */
    @Override
    public List<ShopHomeMiddleCategory> queryAllShopHomeMiddleCategory(Map<String, Object> params) {
        return shopHomeMiddleCategoryDAO.query("getNodes",params);
    }

    /**
     * 查询SHOP_首页中间分类全部节点，通常有最多取30000条记录的限制
     *
     * @return 所有的SHOP_首页中间分类，通常有最多取30000条记录的限制
     */
    @Override
    public List<ShopHomeMiddleCategory> queryShopHomeMiddleCategoryAll(Map<String, Object> params) {
        return shopHomeMiddleCategoryDAO.query("getNodesAll",params);
    }

    /**
     * 查询SHOP_首页中间分类
     *
     * @param params 查询参数，为key-value形式
     * @return SHOP_首页中间分类集合
     */
    @Override
    public List<ShopHomeMiddleCategory> queryShopHomeMiddleCategory(Map<String, Object> params) {
        return shopHomeMiddleCategoryDAO.query(params);
    }

    /**
     * 查询SHOP_首页中间分类
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_首页中间分类集合
     */
    @Override
    public List<ShopHomeMiddleCategory> queryShopHomeMiddleCategory(Map<String, Object> params, int startRow, int rowSize) {
        return shopHomeMiddleCategoryDAO.query(params, startRow, rowSize);
    }

    /**
     * 统计SHOP_首页中间分类
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countShopHomeMiddleCategory(Map<String, Object> params) {
        return shopHomeMiddleCategoryDAO.count(params);
    }

    /**
     * 根据父id查询SHOP_首页中间分类
     * @param param
     * @return
     */
	@Override
	public List<ShopHomeMiddleCategory> queryParentHomeMiddleCategory(Map<String, Object> param) {
		return shopHomeMiddleCategoryDAO.query(param);
	}


}